package com.example.my_isc_nirscan.adapter;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.my_isc_nirscan.R;
import java.util.List;
/**
 * 蓝牙设备列表适配器---testBluetoothActivity
 */
public class BluetoothDeviceAdapter extends BaseAdapter {

    private Context context;
    private List<BluetoothDevice> deviceList;

    public BluetoothDeviceAdapter(Context context, List<BluetoothDevice> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    @Override
    public int getCount() {
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.bluetooth_device_item, parent, false);
            holder = new ViewHolder();
            holder.tvName = convertView.findViewById(R.id.tv_device_name);
            holder.tvAddress = convertView.findViewById(R.id.tv_device_address);
            holder.tvState = convertView.findViewById(R.id.tv_device_status);
            holder.tvRssi = convertView.findViewById(R.id.tv_device_rssi);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BluetoothDevice device = deviceList.get(position);

        // 设置设备名称
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            holder.tvName.setText("无权限获取名称");
            holder.tvAddress.setText("无权限获取地址");
            holder.tvState.setText("无权限获取状态");
            return convertView;
        }
        String name = device.getName();
        if (TextUtils.isEmpty(name)) {
            name = "未知设备";
        }
        holder.tvName.setText(name);
        holder.tvName.setGravity(Gravity.CENTER);
        holder.tvName.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4));
        holder.tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        holder.tvName.setTextColor(Color.BLACK);

        // 设置设备地址
        holder.tvAddress.setText(device.getAddress());
        holder.tvAddress.setGravity(Gravity.CENTER);
        holder.tvAddress.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 5));
        holder.tvAddress.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        holder.tvAddress.setTextColor(Color.BLACK);

        // 设置设备状态
        String stateText = "";
        if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
            int bondState = device.getBondState();
            switch (bondState) {
                case BluetoothDevice.BOND_NONE:
                    stateText = "未配对";
                    break;
                case BluetoothDevice.BOND_BONDING:
                    stateText = "配对中";
                    break;
                case BluetoothDevice.BOND_BONDED:
                    stateText = "已配对";
                    break;
            }
        } else {
            stateText = "无权限获取状态";
        }
        holder.tvState.setText(stateText);
        holder.tvState.setGravity(Gravity.CENTER);
        holder.tvState.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2));
        holder.tvState.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        holder.tvState.setTextColor(Color.BLACK);

        // 设置信号强度
//        int rssi = device.get
//        holder.tvRssi.setText(String.valueOf(rssi));
        holder.tvRssi.setGravity(Gravity.CENTER);
        holder.tvRssi.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1));
        holder.tvRssi.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        holder.tvRssi.setTextColor(Color.BLACK);


        return convertView;
    }

    static class ViewHolder {
        TextView tvName;
        TextView tvAddress;
        TextView tvState;
        TextView tvRssi;
    }
}
